<?php

class media {

	private $id;
	private $idUtilisateur;
	private $type;
	private $src;
	private $titre;
	private $contenu;
	
	public function __construct(){
		$type = "";
		$src = "";
		$titre = "";
		$contenu = "";
	}
	
	public function getId() {return $this->id;}
	public function getIdUtilisateur() {return $this->idUtilisateur;}
	public function getType() {return $this->type;}
	public function getSrc() {return $this->src;}
	public function getTitre() {return $this->titre;}
	public function getContenu() {return $this->contenu;}
	
	public function setId($x) {$this->id = $x;}
	public function setIdUtilisateur($x) {$this->idUtilisateur = $x;}
	public function setType($x) {$this->type = $x;}
	public function setSrc($x) {$this->src = $x;}
	public function setTitre($x) {$this->titre = $x;}
	public function setContenu($x) {$this->contenu = $x;}
	
	static public function getByArray($Array) {
		if(is_array($Array)){
			try {
				$result = new media();
				$result->setId(				(integer)$Array['ID']);
				$result->setIdUtilisateur(	(integer)$Array['ID_UTILISATEUR']);
				$result->setType(			(string)$Array['TYPE']);
				$result->setSrc(			(string)$Array['SRC']);
				$result->setTitre(			(string)$Array['TITRE']);
				$result->setContenu(		(string)$Array['CONTENU']);
			} catch (Exception $e){
				$result = null;
			}
				} else {
			throw new InvalidArgumentException();
		}
		return $result;
	}
	
	public function getMediaByUser($user){
		$DB = DB::getInstance();
		//$resultArray[];// = new array();
		$result = new media();
		$sql = "SELECT * ";
		$sql.= "FROM MEDIA ";
		$sql.= "WHERE ID_UTILISATEUR = '".$user."'";
		/**/
		$stm = $DB->query($sql);
		$i = 0;
		if($stm->rowCount()>0){
			foreach ($DB->query($sql) as $row){
				try{
					$result = self::getByArray($row);
					$resultArray[] = $result;
					$i++;
				}catch (Exception $e){
					$result[] = null;
				}
			}
		}
		return $result;
	}
	
	public function add(){
		$DB = DB::getInstance();
		$sql = "INSERT INTO MEDIA (ID_UTILISATEUR, TYPE, SRC, TITRE, CONTENU) ";
		$sql.= "VALUES ('".$this->idUtilisateur."', '".$this->type."', '".$this->src."', '".$this->titre."', '".$this->contenu."' );";
		$stm = $DB->exec($sql);
		if($stm != false)
		return true;
		else return false;
	}
	
	public function delete(){
		$DB = DB::getInstance();
		$sql = "DELETE FROM MEDIA WHERE ID = '".$this->id."';";
		$stm = $DB->exec($sql);
		if($stm != false)
		return true;
		else return false;
	}
		
		

}